#ifndef 	MYIPMSG_H

#define 	MYIPMSG_H
/*------------------------------------------包含头文件---------------------------------------*/
#include 	<stdio.h>
#include 	<stdlib.h>
#include 	<string.h>
#include 	<sys/types.h>
#include 	<sys/socket.h>
#include 	<sys/stat.h>
#include 	<readline/readline.h>
#include 	<readline/history.h>
#include 	<errno.h>
#include 	<unistd.h>
#include 	<time.h>
#include 	<utime.h>
#include 	<netinet/in.h>
#include 	<arpa/inet.h>
#include 	<pthread.h>
#include 	<limits.h>
#include 	<math.h>
#include	<signal.h>
#include 	<fcntl.h>
#include 	<sys/ioctl.h>
#include	<dirent.h>
/*-----------------------------------------------------------------------------------------*/
/*------------------------------------------常值宏--------------------------------------*/
#define 	MAX_USER_NAME_LENGTH 	256
#define 	MAX_USER_GROUP_LENGTH 	256
#define 	HOSTNAME_LENGTH 	256
#define 	MAXSOCKBUF 		8192
#define 	DEFAULTPORT 		2425
#define 	MAXTRANSFILENUM		100
#define 	MAXFILESEND		20
#define 	MAXREPEATTIME		3	/*最长的等待时间*/
#define		MAXCOMMANDLINELEN	100	/*输入命令的总长度*/
#define		MAX_APPEND_LEN		8192	/*附加区域的最大长度*/
#define		MAX_SEND_FILE_HEADER_LEN 0xffff	/*发送文件的头部的最大长度*/
/*----------------------------------------------------------------------------------------*/
/*--------------------------------------------数据结构定义------------------------------------------------*/
typedef struct ipmsguser
{
	int 			user_num;	/*在线用户编号*/
	struct sockaddr_in 	user_addr;
	char 			user_name[MAX_USER_NAME_LENGTH];
	char 			user_group[MAX_USER_GROUP_LENGTH];
	char 			user_machine[HOSTNAME_LENGTH];
	char 			append[MAXSOCKBUF];
	char 			user_nick[MAX_USER_NAME_LENGTH];
	struct ipmsguser 	* next;
	struct ipmsguser 	* pre;
}ipmsg_user_list;

typedef struct ipmsgdata
{
	int 		version;
	unsigned int 	packetno;
	char 		sendername[MAX_USER_NAME_LENGTH];
	char		*ip;
	char 		senderhost[HOSTNAME_LENGTH];
	unsigned int 	com;
	char 		append[MAX_APPEND_LEN];
	char 		sendfileheader[MAX_SEND_FILE_HEADER_LEN];
}ipmsgdata;

typedef struct transfileheader
{
	unsigned int 		fileID;
	char 			filename[PATH_MAX];
	unsigned long long 	size;
	unsigned int 		mtime;
	unsigned long 		fileattr;
}transfileheader;

typedef struct transfolderheader
{
	int 			headersize;
	char 			filename[PATH_MAX];
	unsigned long long 	filesize;
	unsigned long 		fileattr;
	time_t 			filemtime;	//文件/文件夹的修改和创建时间
	time_t 			filectime;
}transfolderheader;

typedef struct sendfilearg
{
	int		filenum;
	int 		flags;	/*flags用来记录发送到的目的地的形式：1为序号，2为all，3为IP,4为用户名*/
	char 		filename[MAXFILESEND][PATH_MAX];	//文件/文件夹名
	unsigned long	fileattr[MAXFILESEND];	/*发送的文件的属性，为标志量，
						 *为0时表示对应的文件名为文件，为1表示对应为目录*/
	char 		to[20];	//目的地
}sendfilearg;

/*---------------------------------------------------------------------------------------------*/
#ifndef 	MYMAIN_C
/*---------------------------------全局变量声明------------------------------------------------*/
extern char 			buffer[MAXSOCKBUF*2];
extern char 			hostname[HOSTNAME_LENGTH];
extern char 			buff[8192];
extern char 			* user;
extern char 			* group;
extern int 			soc;
extern struct sockaddr_in 	me;
extern struct sockaddr_in 	me_tcp;
extern struct sockaddr_in 	from;
extern socklen_t 			fromlen;
extern int 			ipmsgport;
extern char 			bcastaddrstr[];
extern struct ipmsguser 	* head, * tail;
extern struct sockaddr_in 	bcast;
extern pthread_t 		id;		/*用于网络监控*/
extern pthread_t 		id_getfiles;	/*用于建立TCP链接，进行文件传输*/
extern pthread_t 		id_sendfiles;	/*发送文件/文件夹*/
extern int 			trans_file_socs[8];	/*最多只能同时8个socket进行文件传输*/
extern struct sockaddr_in 	trans_file_from[8];	/*8个用户同时向本机传文件*/
extern transfileheader 		files[MAXTRANSFILENUM];
extern int 			filenum;
extern ipmsgdata 		* datafortcp;
extern unsigned int 		pocketnos;		/*用来记录发送出去的包的编号，用来和返回的数据包的编号进行核对*/
extern int 			sendcheckflag;		/*标记发送命令中是否含有发送检查标志，1为含有，0为不含*/
extern int 			repeatflag;			/*是否重发标志*/
extern int 			recvflag;
extern int			arrivetimeflag;	/*检查是否到时，以确定是否重发*/
extern int			startsendflag;	/*开始发送标志*/
extern int			FileID;			/*记录对方请求发送的文件号*/
/*----------------------------------------------------------------------------------------------*/
#endif	/*end of MYMAIN_C*/
/*----------------------------------函数声明----------------------------------------------------*/
int 		command(char *cmd);
void 		ipmsgswitch(ipmsgdata * data, char * buff);
void 		ipmsgsendbcast(struct sockaddr_in * dest, unsigned int com, ipmsgdata * data);
ipmsg_user_list * adduser(struct sockaddr_in * useraddrs, struct ipmsguser * q, ipmsgdata * data);
char 		user_exist(struct sockaddr_in * useraddr, ipmsgdata * data);
void 		* watch_net(void * arg);
void 		recvfromx(int signo);
void 		del_user(struct sockaddr_in * useraddr, ipmsgdata * data);
void 		del_all_user();
ipmsg_user_list * search_user(int * usernum, struct in_addr * useraddr, char * username);
void 		* getfiles(void * arg);
void 		* sendfiles(void * arg);
char 		* mystrtok (char * s, char * delim, size_t * olds);
int 		senddir(char dir[], int socksendfd);
/*---------------------------------------------------------------------------------------------*/

#endif	/*end of MYIPMSG_H*/
